/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.fileInfo;

/**
 *
 * @author user
 */
public class DbHandler {

    protected Connection connect = null;
    protected Statement statement = null;
    protected PreparedStatement preparedStatement = null;
    protected ResultSet resultSet = null;
    protected String dbname;
    protected String mysqluser;
    protected String mysqlpass;

    public DbHandler() {
        try {
            this.dbname = "anjync";
            this.mysqluser = "root";
            this.mysqlpass = "";
            this.connect();
            this.statement = this.connect.createStatement();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DbHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(DbHandler.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void connect() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        connect = DriverManager.getConnection("jdbc:mysql://localhost/" + dbname + "?user=" + mysqluser + "&password=" + mysqlpass);
    }

    public boolean cekLogin(String usr, String pass) {
        boolean ret = false;
        try {
            String md5pass = pass;
            this.resultSet = statement.executeQuery("SELECT * FROM user WHERE username='" + usr + "' AND password='" + md5pass + "'");
            if (this.countResult(resultSet) == 1) {
                ret = true;
            } else {
            }
        } catch (SQLException ex) {
            Logger.getLogger(DbHandler.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (NoSuchAlgorithmException ex) {
//            System.err.println("No algorithm");
//            Logger.getLogger(DbHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ret;
    }

    public String getUsernameFromUserID(int uid){
        String usr = null;
        try {
            this.resultSet = statement.executeQuery("SELECT username FROM user WHERE user_ID=" + uid);

            while (this.resultSet.next()) {
                usr = this.resultSet.getString("username");
            }

        } catch (SQLException ex) {
            Logger.getLogger(DbHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return usr;
    }

    public int getUserIDfromUsername(String usrname){
        int usrid = -1;
        try {
            this.resultSet = statement.executeQuery("SELECT user_ID FROM user WHERE username='" + usrname +"'");

            while (this.resultSet.next()) {
                usrid = this.resultSet.getInt("user_ID");
            }

        } catch (SQLException ex) {
            Logger.getLogger(DbHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return usrid;
    }

    public ArrayList<fileInfo> getAllFilesInfo(String username){
        ArrayList<fileInfo> fi = new ArrayList<fileInfo>();
        
        try {
            int usr_id = this.getUserIDfromUsername(username);
            this.resultSet = statement.executeQuery("SELECT file_path,date_modified" + " FROM user_files WHERE user_ID=" + usr_id);

            while(this.resultSet.next()){
                fi.add(new fileInfo(this.resultSet.getString("file_path"),
                                    this.resultSet.getString("date_modified")));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DbHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return fi;
    }

    public String convert_md5(String input) throws NoSuchAlgorithmException {
        String output;

        byte[] passbyte = input.getBytes();

        MessageDigest m = MessageDigest.getInstance("MD5");

        m.update(passbyte, 0, input.length());
        output = new BigInteger(1, m.digest()).toString(16);
        return output;
    }

    public int countResult(ResultSet r) throws SQLException {
        int count = 0;
        while (r.next()) {
            count++;
        }

        return count;
    }

    public static void main(String[] args) {

        DbHandler d = new DbHandler();
        if (d.cekLogin("maho", "maho")) {
            System.out.println("valid");
        } else {
            System.out.println("nggak");
        }
    }
}
